```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ArrayList 与 LinkedList 深度对比 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .serif {
            font-family: 'Noto Serif SC', serif;
        }
        .drop-cap::first-letter {
            font-size: 3.5em;
            float: left;
            line-height: 0.8;
            margin-right: 0.1em;
            color: #4f46e5;
        }
        .code-block {
            background-color: #f8f9fa;
            border-left: 4px solid #4f46e5;
            color: #333;
            font-family: monospace;
            overflow-x: auto;
        }
        .hover-grow {
            transition: transform 0.3s ease;
        }
        .hover-grow:hover {
            transform: translateY(-5px);
        }
        .gradient-text {
            background: linear-gradient(90deg, #4f46e5, #10b981);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="relative bg-gradient-to-r from-indigo-600 to-emerald-500 text-white py-20 md:py-32 px-4 sm:px-6 lg:px-8">
        <div class="max-w-5xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl lg:text-6xl font-bold mb-6 serif">
                <span class="gradient-text">ArrayList</span> 与 <span class="gradient-text">LinkedList</span>
            </h1>
            <p class="text-xl md:text-2xl mb-8 max-w-3xl mx-auto">
                Java集合框架中的经典对决：从源码角度深度解析两者实现差异与性能对比
            </p>
            <div class="flex justify-center space-x-4">
                <a href="#comparison" class="px-6 py-3 bg-white text-indigo-600 font-medium rounded-full hover:bg-gray-100 transition duration-300">
                    <i class="fas fa-balance-scale mr-2"></i>性能对比
                </a>
                <a href="#summary" class="px-6 py-3 bg-indigo-700 text-white font-medium rounded-full hover:bg-indigo-800 transition duration-300">
                    <i class="fas fa-lightbulb mr-2"></i>适用场景
                </a>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0 h-16 bg-gradient-to-t from-gray-50 to-transparent"></div>
    </section>

    <!-- Main Content -->
    <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="text-xl text-gray-700 mb-8 drop-cap">
                对比 ArrayList 和 LinkedList 的实现，可以帮助我们理解两者在不同场景下的优缺点，以及在选择集合类时如何做出更合适的决策。ArrayList 和 LinkedList 都实现了 List 接口，但底层数据结构和操作实现方式完全不同，因此它们在性能和使用场景上有很大的差异。下面从源码角度详细分析 ArrayList 和 LinkedList 的实现。
            </p>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-md overflow-hidden hover-grow">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="w-12 h-12 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                                <i class="fas fa-table text-indigo-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-bold text-gray-800">ArrayList</h3>
                        </div>
                        <p class="text-gray-600 mb-4">
                            基于动态数组实现，适合用于频繁访问元素的场景。底层存储是一个数组，访问元素的时间复杂度为 O(1)，但插入和删除元素时可能需要移动数组中的元素，时间复杂度为 O(n)。
                        </p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-indigo-100 text-indigo-800 text-sm rounded-full">随机访问快</span>
                            <span class="px-3 py-1 bg-indigo-100 text-indigo-800 text-sm rounded-full">连续内存</span>
                            <span class="px-3 py-1 bg-indigo-100 text-indigo-800 text-sm rounded-full">扩容机制</span>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md overflow-hidden hover-grow">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="w-12 h-12 rounded-full bg-emerald-100 flex items-center justify-center mr-4">
                                <i class="fas fa-link text-emerald-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-bold text-gray-800">LinkedList</h3>
                        </div>
                        <p class="text-gray-600 mb-4">
                            基于双向链表实现，适合用于频繁插入和删除元素的场景。每个元素都是一个节点，插入和删除元素时不需要移动其他元素，时间复杂度为 O(1)，但访问元素时需要遍历链表，时间复杂度为 O(n)。
                        </p>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-3 py-1 bg-emerald-100 text-emerald-800 text-sm rounded-full">插入删除快</span>
                            <span class="px-3 py-1 bg-emerald-100 text-emerald-800 text-sm rounded-full">节点链接</span>
                            <span class="px-3 py-1 bg-emerald-100 text-emerald-800 text-sm rounded-full">无需扩容</span>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- ArrayList Implementation -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 serif flex items-center">
                <span class="w-8 h-8 bg-indigo-600 text-white rounded-full flex items-center justify-center mr-4">1</span>
                ArrayList 源码实现
            </h2>
            <p class="text-gray-700 mb-6">
                ArrayList 底层是基于数组实现的，因此它在初始化时会分配一个数组，当元素数量超过数组的容量时会进行扩容。
            </p>
            
            <div class="mb-8">
                <h3 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-code-branch text-indigo-500 mr-2"></i>初始化与扩容机制
                </h3>
                <div class="code-block p-4 rounded-lg mb-4">
                    <pre><code class="language-java">public class ArrayList&lt;E&gt; extends AbstractList&lt;E&gt; 
    implements List&lt;E&gt;, RandomAccess, Cloneable, java.io.Serializable {
    private static final int DEFAULT_CAPACITY = 10;  // 默认容量
    private Object[] elementData;  // 存储元素的数组
    private int size = 0;  // 当前集合的大小

    public ArrayList() {
        this.elementData = new Object[DEFAULT_CAPACITY];  // 初始化数组
    }

    public boolean add(E e) {
        ensureCapacity(size + 1);  // 确保容量足够
        elementData[size++] = e;  // 将元素添加到数组中
        return true;
    }

    // 扩容机制
    private void ensureCapacity(int minCapacity) {
        if (minCapacity - elementData.length > 0) {
            grow(minCapacity);  // 扩容
        }
    }

    // 扩容的实现
    private void grow(int minCapacity) {
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);  // 扩容1.5倍
        elementData = Arrays.copyOf(elementData, newCapacity);  // 拷贝原数组到新数组
    }
}</code></pre>
                </div>
                <div class="bg-blue-50 border-l-4 border-blue-400 p-4">
                    <div class="flex">
                        <div class="flex-shrink-0">
                            <i class="fas fa-info-circle text-blue-400"></i>
                        </div>
                        <div class="ml-3">
                            <h3 class="text-sm font-medium text-blue-800">扩容说明</h3>
                            <div class="mt-2 text-sm text-blue-700">
                                <p>• <code>ensureCapacity</code> 用于判断当前数组的容量是否足够，如果不够就调用 <code>grow</code> 方法扩容。</p>
                                <p>• <code>grow</code> 方法通过 <code>Arrays.copyOf</code> 创建一个更大的数组，并将原有数组的元素拷贝过去，扩容后的容量为原容量的 1.5 倍。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- LinkedList Implementation -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 serif flex items-center">
                <span class="w-8 h-8 bg-emerald-600 text-white rounded-full flex items-center justify-center mr-4">2</span>
                LinkedList 源码实现
            </h2>
            <p class="text-gray-700 mb-6">
                LinkedList 底层是基于双向链表实现的，每个元素都是一个节点，每个节点包含数据和指向前后节点的指针。插入和删除元素时，只需要修改指针，而不需要移动其他元素。
            </p>
            
            <div class="mb-8">
                <h3 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-project-diagram text-emerald-500 mr-2"></i>节点定义与插入操作
                </h3>
                <div class="code-block p-4 rounded-lg mb-4">
                    <pre><code class="language-java">public class LinkedList&lt;E&gt; extends AbstractSequentialList&lt;E&gt; 
    implements List&lt;E&gt;, Deque&lt;E&gt;, Cloneable, java.io.Serializable {
    transient Node&lt;E&gt; first;  // 链表头
    transient Node&lt;E&gt; last;   // 链表尾
    private int size = 0;     // 当前链表的元素数量

    // 节点定义
    private static class Node&lt;E&gt; {
        E item;
        Node&lt;E&gt; next;
        Node&lt;E&gt; prev;

        Node(Node&lt;E&gt; prev, E element, Node&lt;E&gt; next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

    public boolean add(E e) {
        linkLast(e);  // 将元素添加到链表末尾
        return true;
    }

    // 链接新节点到链表末尾
    void linkLast(E e) {
        final Node&lt;E&gt; l = last;
        final Node&lt;E&gt; newNode = new Node&lt;&gt;(l, e, null);
        last = newNode;
        if (l == null)  // 如果链表为空，设置first
            first = newNode;
        else
            l.next = newNode;
        size++;
    }
}</code></pre>
                </div>
                <div class="bg-green-50 border-l-4 border-green-400 p-4">
                    <div class="flex">
                        <div class="flex-shrink-0">
                            <i class="fas fa-info-circle text-green-400"></i>
                        </div>
                        <div class="ml-3">
                            <h3 class="text-sm font-medium text-green-800">节点插入操作</h3>
                            <div class="mt-2 text-sm text-green-700">
                                <p>• <code>linkLast</code> 方法将新的元素插入到链表的末尾。</p>
                                <p>• <code>Node</code> 类表示链表的节点，包含数据元素 <code>item</code> 和指向前后节点的指针 <code>prev</code> 和 <code>next</code>。</p>
                                <p>• 链表的头节点 <code>first</code> 和尾节点 <code>last</code> 用于维护链表的开始和结束。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Performance Comparison -->
        <section id="comparison" class="mb-16">
            <h2 class="text-3xl font-bold text-gray-800 mb-8 serif">性能对比</h2>
            
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="bg-white p-6 rounded-xl shadow-sm border border-gray-100">
                    <h3 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-plus-circle text-indigo-500 mr-2"></i>添加元素
                    </h3>
                    <div class="space-y-4">
                        <div>
                            <div class="flex justify-between mb-1">
                                <span class="text-sm font-medium text-indigo-700">ArrayList</span>
                                <span class="text-sm font-medium text-gray-500">O(1)~O(n)</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2.5">
                                <div class="bg-indigo-600 h-2.5 rounded-full" style="width: 70%"></div>
                            </div>
                            <p class="mt-1 text-xs text-gray-500">
                                末尾添加O(1)，数组满时扩容O(n)
                            </p>
                        </div>
                        <div>
                            <div class="flex justify-between mb-1">
                                <span class="text-sm font-medium text-emerald-700">LinkedList</span>
                                <span class="text-sm font-medium text-gray-500">O(1)</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2.5">
                                <div class="bg-emerald-600 h-2.5 rounded-full" style="width: 100%"></div>
                            </div>
                            <p class="mt-1 text-xs text-gray-500">
                                直接修改指针，无需扩容
                            </p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm border border-gray-100">
                    <h3 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-search text-indigo-500 mr-2"></i>查找元素
                    </h3>
                    <div class="space-y-4">
                        <div>
                            <div class="flex justify-between mb-1">
                                <span class="text-sm font-medium text-indigo-700">ArrayList</span>
                                <span class="text-sm font-medium text-gray-500">O(1)</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2.5">
                                <div class="bg-indigo-600 h-2.5 rounded-full" style="width: 100%"></div>
                            </div>
                            <p class="mt-1 text-xs text-gray-500">
                                直接通过索引访问数组元素
                            </p>
                        </div>
                        <div>
                            <div class="flex justify-between mb-1">
                                <span class="text-sm font-medium text-emerald-700">LinkedList</span>
                                <span class="text-sm font-medium text-gray-500">O(n)</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2.5">
                                <div class="bg-emerald-600 h-2.5 rounded-full" style="width: 30%"></div>
                            </div>
                            <p class="mt-1 text-xs text-gray-500">
                                需要从头到尾遍历链表
                            </p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm border border-gray-100">
                    <h3 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-trash-alt text-indigo-500 mr-2"></i>删除元素
                    </h3>
                    <div class="space-y-4">
                        <div>
                            <div class="flex justify-between mb-1">
                                <span class="text-sm font-medium text-indigo-700">ArrayList</span>
                                <span class="text-sm font-medium text-gray-500">O(n)</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2.5">
                                <div class="bg-indigo-600 h-2.5 rounded-full" style="width: 30%"></div>
                            </div>
                            <p class="mt-1 text-xs text-gray-500">
                                需要移动删除位置后的所有元素
                            </p>
                        </div>
                        <div>
                            <div class="flex justify-between mb-1">
                                <span class="text-sm font-medium text-emerald-700">LinkedList</span>
                                <span class="text-sm font-medium text-gray-500">O(1)~O(n)</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2.5">
                                <div class="bg-emerald-600 h-2.5 rounded-full" style="width: 70%"></div>
                            </div>
                            <p class="mt-1 text-xs text-gray-500">
                                修改指针O(1)，但查找节点O(n)
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mb-8">
                <div class="mermaid">
                    graph TD
                    A[操作类型] --> B[添加元素]
                    A --> C[查找元素]
                    A --> D[删除元素]
                    
                    B --> E[ArrayList: O(1)~O(n)]
                    B --> F[LinkedList: O(1)]
                    
                    C --> G[ArrayList: O(1)]
                    C --> H[LinkedList: O(n)]
                    
                    D --> I[ArrayList: O(n)]
                    D --> J[LinkedList: O(1)~O(n)]
                </div>
            </div>
        </section>

        <!-- Summary -->
        <section id="summary" class="mb-16">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 serif">适用场景总结</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-indigo-50 rounded-xl p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                            <i class="fas fa-check-circle text-indigo-600"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">ArrayList</h3>
                    </div>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                            <span>适合频繁访问元素的场景，尤其是需要随机访问时</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                            <span>内存占用较小，因为不需要存储节点指针</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-2"></i>
                            <span>在大量插入和删除操作时性能较差，因为需要移动数组元素</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-emerald-50 rounded-xl p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-emerald-100 flex items-center justify-center mr-4">
                            <i class="fas fa-check-circle text-emerald-600"></i>
                        </div>
                        <h3 class="text-xl font-bold text-gray-800">LinkedList</h3>
                    </div>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check text-emerald-500 mt-1 mr-2"></i>
                            <span>适合频繁插入和删除操作的场景，尤其是操作链表两端时</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-emerald-500 mt-1 mr-2"></i>
                            <span>不需要连续内存，可以更有效地利用内存碎片</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-2"></i>
                            <span>在大量访问元素时性能较差，因为需要逐个节点遍历</span>
                        </li>
                    </ul>
                </div>
            </div>
            
            <div class="mt-8 bg-white p-6 rounded-xl shadow-sm">
                <h3 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>选择建议
                </h3>
                <div class="space-y-4">
                    <p class="text-gray-700">
                        <span class="font-medium text-indigo-600">使用 ArrayList 的场景：</span>
                        当你需要频繁随机访问元素（get和set操作）时，ArrayList 是更好的选择，它的时间复杂度为 O(1)。例如，需要实现一个需要频繁访问元素的缓存系统。
                    </p>
                    <p class="text-gray-700">
                        <span class="font-medium text-emerald-600">使用 LinkedList 的场景：</span>
                        当你需要频繁在列表中间插入和删除元素时，LinkedList 是更好的选择，它的时间复杂度为 O(1)。例如，实现一个队列或双向队列（Deque）。
                    </p>
                    <p class="text-gray-700">
                        <span class="font-medium text-purple-600">其他考虑：</span>
                        在实际应用中，还需要考虑内存使用情况。ArrayList 通常比 LinkedList 更节省内存，因为 LinkedList 需要为每个元素存储两个额外的引用（前驱和后继节点）。
                    </p>
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-12">
        <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col items-center">
                <div class="text-xl font-bold mb-2">技术小馆</div>
                <div class="text-gray-400 mb-6">探索技术的无限可能</div>
                <a href="http://www.yuque.com/jtostring" class="text-emerald-400 hover:text-emerald-300 transition duration-300">
                    <i class="fas fa-external-link-alt mr-2"></i>www.yuque.com/jtostring
                </a>
                <div class="mt-8 pt-8 border-t border-gray-800 w-full text-center text-gray-500 text-sm">
                    &copy; 2023 技术小馆. 保留所有权利.
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```